import {
  createPageData,
  deletePageData,
  editPageData,
  getPageListData
} from '@/service/main/system/system';
import { IRootState } from '@/store/types';
import { Module } from 'vuex';

import { ISystemState } from './types';

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      userList: [],
      userCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0
    };
  },
  mutations: {
    changeUserList(state, list: any[]) {
      state.userList = list;
    },
    changeUserCount(state, count: number) {
      state.userCount = count;
    },
    changeRoleList(state, list: any[]) {
      state.roleList = list;
    },
    changeRoleCount(state, count: number) {
      state.roleCount = count;
    },
    changeGoodsList(state, list: any[]) {
      state.goodsList = list;
    },
    changeGoodsCount(state, count: number) {
      state.goodsCount = count;
    },
    changeMenuList(state, list: any[]) {
      state.menuList = list;
    },
    changeMenuCount(state, count: number) {
      state.menuCount = count;
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      // 1.根据页面调用不同的接口列表
      const pageName = payload.pageName;
      const pageUrl = `/${pageName}/list`;
      const pageResult = await getPageListData(pageUrl, payload.queryInfo);
      const { list, total } = pageResult.data;
      // 2.然后调用不同的Mutation存储数据
      const changePageName = pageName.slice(0, 1).toUpperCase() + pageName.slice(1);
      commit(`change${changePageName}List`, list);
      commit(`change${changePageName}Count`, total);
    },
    async deletePageDataAction({ dispatch }, payload: any) {
      // pageName->user
      // id->1
      const { pageName, id } = payload;
      const pageUrl = `/${pageName}/${id}`;
      await deletePageData(pageUrl);
      // 重新请求获取数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: { offset: 1, size: 10 }
      });
    },
    async createPageDataAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload;
      const pageUrl = `/${pageName}`;
      await createPageData(pageUrl, newData);
      dispatch('getPageListAction', {
        pageName,
        queryInfo: { offset: 1, size: 10 }
      });
    },
    async editPageDataAction({ dispatch }, payload: any) {
      const { pageName, newData, id } = payload;
      const pageUrl = `/${pageName}`;
      await editPageData(pageUrl, newData);
      dispatch('getPageListAction', {
        pageName,
        queryInfo: { offset: 1, size: 10 }
      });
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`];
      };
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`];
      };
    }
  }
};

export default systemModule;
